/**
 * Created by shc@lierda.com on 2017/2/22.
 */
var React = require('react');

var Icon = require('../components/Icon');
var ajax = require('../tools/ajax');
var IncreaseId = require('../../web/tools/IncreaseId');
var MsgBox = require('../components/MsgBox');

module.exports = React.createClass({
    getInitialState: function (){
        return {
            title:"",//头部标题
            lt:"",//左部内容
            rt:"",//右部内容
            comment:"",
            photoUrl:[]//评论图片集合
        }
    },
    componentDidMount: function () {
        setTimeout(this.attr,500);
        window.goBack=this.goBack;
    },

    /**安卓返回键功能*/
    goBack(){
        location.href = "#wode";
    },
    //
    attr:function(){
        if(this.props.title){
            this.setState({title:this.props.title});
        }
        if(this.props.lt){
            this.setState({lt:this.props.lt});
        }
        if(this.props.rt){
            this.setState({rt:this.props.rt});
        }
        if(this.props.comment){
            this.setState({comment:this.props.comment});
        }
    },
    action:function(e){
        var action = e.currentTarget.getAttribute("action");
        location.href = action;
    },

    //发布评论上传到数据库
    release(){
        var id = IncreaseId.increaseId();
        var user_id = localStorage.getItem("userId");
        var add_time = new Date().getTime();
        var content = this.state.comment;
        var photo = this.state.photoUrl;
        console.log(content);
        console.log(photo);
        if(content && photo.length > 0){
            ajax("POST","releaseComment",{
                id:id,
                user_id:user_id,
                content:content,
                add_time:add_time,
                photo:JSON.stringify(photo),
            },function(dt){
                console.log(dt);
            })
            location.href = "#wode";
        }else{
            this.refs.MsgBox.setState({
                type: "no",
                timeout:1000,
                show:true,
                word:"请填写评论并上传图片"
            })
        }
    },

    render: function () {
        return (
            <div className="Cheader">
                <div className="left" onClick={this.goBack}>
                    {this.state.lt}
                </div>
                <div className="title">
                    {this.state.title}
                </div>
                <div className="right" onClick={this.release}>
                    {this.state.rt}
                </div>
                <MsgBox ref="MsgBox"></MsgBox>
            </div>
        )
    }
});
